package com.eazyshop.search.controller;

import com.eazyshop.common.pojo.Result;
import com.eazyshop.search.service.imp.SearchServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@RestController
@RequestMapping("/search")
@CrossOrigin
public class QueryController {

    @Autowired
    private SearchServiceImpl searchService;


    @GetMapping
    public Result  queryByConditions(@RequestParam Map<String, String> queryMap){

        // 因为是发送的get请求，会对中文字符以及特殊字符进行转码，需要特殊处理
        handleMap(queryMap);
        Map map = searchService.querySkuInfoByQueryMap(queryMap);
        return new Result("查询成功",map);
    }

    /**
     * 对于请求参数中的乱码进行处理
     * @param queryMap
     */
    private void handleMap(Map<String, String> queryMap) {

        for (Map.Entry<String, String> entry : queryMap.entrySet()) {
            if (entry.getKey().startsWith("spec")){
                // 替换特殊符号
                String replace = entry.getValue().replace("+", "%2b");
                queryMap.put(entry.getKey(),replace);

            }
        }

    }

}
